<template>
	<ConfModuleContainer ref="refConfModuleContainer" class="MQTT-container">
		<template #Slot-Conf-Module-Header>
			<p class="font-20-bold bread-crumb-title" style="color: #f0f6fc">MQTT配置</p>
		</template>

		<template #Slot-Conf-Module-Content>
			<div style="display: flex">
				<div class="the-MQTT-config">
					<div class="font-16-bold the-common-title">
						<global-svg-icon icon-name="config-title-tri" width="3px" height="18px" style="margin-right: 16px" />
						<div>MQTT信息</div>
					</div>
					<div class="the-MQTT-form">
						<a-form ref="formRef" :model="formData" name="MQTT" layout="vertical" :rules="addRules">
							<a-form-item name="address" label="地址" style="margin-top: -32px">
								<a-input v-model:value="formData.address" :maxlength="100" placeholder="请输入" style="width: 320px; height: 36px" />
							</a-form-item>
							<a-form-item name="port" label="端口">
								<a-input v-model:value="formData.port" :maxlength="10" placeholder="请输入" style="width: 320px; height: 36px" />
							</a-form-item>
							<a-form-item name="account" label="账号">
								<a-input v-model:value="formData.account" :maxlength="50" placeholder="请输入" style="width: 320px; height: 36px" />
							</a-form-item>
							<a-form-item name="password" label="密码">
								<a-input-password
									v-model:value="formData.password"
									:maxlength="50"
									placeholder="请输入"
									style="width: 320px; height: 36px"
								/>
							</a-form-item>

							<a-form-item style="margin-top: 32px">
								<a-button type="primary" html-type="submit" class="form-button" :loading="loading" @click="addDeviceSubmit">保存</a-button>
							</a-form-item>
						</a-form>
					</div>
				</div>
			</div>
		</template>
	</ConfModuleContainer>
</template>

<script setup lang="ts" name="MQTT-form">
import { VerifyForm } from '@/global-utils/check';
import { Rule } from 'ant-design-vue/es/form';
import { ConfModuleContainer } from '../../components/index';
import { MQTTInfoAPI } from '../../api';
// const route = useRoute();
const $message: any = inject('$message');
const router = useRouter();
const formRef = ref();
// 表单校验
const addRules: Record<string, Rule[]> = {
	address: [{ required: true, message: '请输入链接地址', trigger: 'blur' }],
	port: [{ required: true, message: '请输入端口号', trigger: 'blur' }],
	account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
	password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};

// 表单数据
const formData = reactive<any>({});
const loading = ref<boolean>(false);

// 添加
const addDeviceSubmit = (): void => {
	// console.log('dynamicValidateForm.users:', formData.users);
	console.log(formRef.value);

	VerifyForm(formRef, async () => {
		const newForm = JSON.parse(JSON.stringify(formData));
		loading.value = true;
		try {
			// 接口
			let res: TsRes = await MQTTInfoAPI.save(newForm);
			if (res.code === 0) {
				$message.success('保存成功');
			}
		} finally {
			loading.value = false;
		}
	});
};
const getMqttInfo = async (): Promise<void> => {
	let res: TsRes = await MQTTInfoAPI.MQTTInfo();
	console.log(res.data);
	formData.account = res.data.account;
	formData.address = res.data.address;
	formData.port = res.data.port;
	formData.password = res.data.password;
};
onMounted(() => {
	getMqttInfo();
});
</script>

<style lang="less">
.MQTT-container {
	.ant-select-selector,
	.ant-input-password {
		border: 1px solid @borderColor02 !important;
		border-radius: 6px !important;
		color: @text01 !important;
		background-color: transparent !important;
	}

	.ant-form {
		.ant-form-item {
			.ant-form-item-label {
				margin-top: 32px;
			}
		}
	}

	.the-common-title {
		display: flex;
		margin-bottom: 16px;
		color: @text01;
		align-items: center;
	}

	.the-MQTT-config {
		margin: 0 auto;

		.the-MQTT-form {
			width: 416px;
			border: @borderColor01;
			border-radius: 4px;
			padding: 20px 32px 40px;
			background: @background02;
			box-shadow: 0 40px 180px 0 rgba(0, 0, 0, 0.35);

			.form-button {
				margin-right: 8px;
			}
		}
	}
}
</style>
